//
//  XYFifthStepViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/9.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "XYFifthStepViewController.h"
#import "XYSixthStepViewController.h"
@interface XYFifthStepViewController ()

@end

@implementation XYFifthStepViewController
static const NSString * KEY_STEP  = @"step5";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNaviBarTitle:self.NewRecordArray.firstObject.name];
    _contentTableView = [[UITableView alloc] initWithFrame:
                         CGRectMake(10, fHeight_Nav + fHeight_StatueBar + 10, fWidth_Current - 20, fHeight_Current - fHeight_Nav + fHeight_StatueBar - 60)];
    _contentTableView.backgroundColor = [UIColor clearColor];
    _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _contentTableView.dataSource = self;
    _contentTableView.delegate = self;
    [self.view addSubview:_contentTableView];
    
    UIImageView*buttomview = [[UIImageView alloc]initWithFrame:CGRectMake(0, fHeight_Current - 50, fWidth_Current, 50)];
    buttomview.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
    [self.view addSubview:buttomview];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 50)];
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment=1;
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=[UIColor lightGrayColor];
    label.text=@"当下是第五步5/6";
    [buttomview addSubview:label];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *strIdentifier = [NSString stringWithFormat:@"strIdentifier%ld",(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:strIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
        
        UILabel *titlelab = [[UILabel alloc]initWithFrame:
                             CGRectMake(10, 0, fWidth_Current - 40, 60)];
        titlelab.textColor=[UIColor whiteColor];
        titlelab.backgroundColor=[UIColor clearColor];
        titlelab.text=@"5、血液样本送至生物样本库时间";
        [cell addSubview:titlelab];
        
        UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, fWidth_Current-20, 1)];
        lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
        [cell addSubview:lineview];
        
        UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(fWidth_Current-20-60, 78, 50, 25)];
        btn1.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
        btn1.layer.cornerRadius=10;
        [btn1 setTitle:@"刷新" forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(reloadtime1) forControlEvents:UIControlEventTouchUpInside];
        btn1.titleLabel.font=[UIFont systemFontOfSize:11];
        btn1.clipsToBounds=YES;
        [cell addSubview:btn1];

        if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
            btn1.hidden = YES;
        }else{
            btn1.hidden = NO;
        }
        
        _dateLabel = [[UILabel alloc]initWithFrame:
                      CGRectMake(10, 60, tableView.frame.size.width - 52, 60)];
        _dateLabel.backgroundColor = [UIColor clearColor];
        _dateLabel.textColor = [UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
        [cell addSubview:_dateLabel];
        
        if ([[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] &&
            [[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] count] > indexPath.row &&
            [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:indexPath.row] objectForKey:KEY_RECORD_ANSWER]) {
            
            _dateLabel.text = [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:indexPath.row] objectForKey:KEY_RECORD_ANSWER];
        }
        
 
    }
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 70.0f;
}

- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView = [[UIView alloc] initWithFrame:
                          CGRectMake(0, 0, tableView.frame.size.width , 50)];
    footerView.backgroundColor = [UIColor clearColor];
    
    if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
        
        UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 10, footerView.frame.size.width, 50)];
        btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
        btn2.layer.cornerRadius=5;
        [btn2 setTitle:@"下一步" forState:UIControlStateNormal];
        btn2.clipsToBounds=YES;
        [btn2 addTarget:self action:@selector(nextStepClick) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:btn2];
    }else{
        UIButton *btn1=[[UIButton alloc]initWithFrame:
                        CGRectMake(0, 10, (footerView.frame.size.width - 10) / 2, 50)];
        btn1.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
        btn1.layer.cornerRadius=5;
        [btn1 setTitle:@"取消" forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
        btn1.clipsToBounds=YES;
        [footerView addSubview:btn1];
        UIButton *btn2=[[UIButton alloc]initWithFrame:
                        CGRectMake(btn1.frame.origin.x + btn1.frame.size.width + 10,
                                   btn1.frame.origin.y,
                                   btn1.frame.size.width, btn1.frame.size.height)];
        btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
        btn2.layer.cornerRadius=5;
        [btn2 setTitle:@"保存" forState:UIControlStateNormal];
        btn2.clipsToBounds=YES;
        [btn2 addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:btn2];
    }
    
    return footerView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)saveClick{
    if(_dateLabel.text == nil || [_dateLabel.text isEqualToString:@""]){
        [self.view makeToast:@"请填写入库时间！"];
        return;
    }
    
    [self creatDetailDict];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (Record *model in self.NewRecordArray) {
            [model update];
        }
    });
    NSMutableArray *types = [NSMutableArray arrayWithCapacity:0];
    for (Record *model in self.NewRecordArray) {
        NSString*step3date = [[[[model getDetailDict] objectForKey:@"step3"] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER];
        NSString*step5date = [[[[model getDetailDict] objectForKey:@"step5"] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
        NSDate *date1=[formatter dateFromString:step3date];
        NSDate *date2=[formatter dateFromString:step5date];
        NSTimeInterval secondsInterval= [date2 timeIntervalSinceDate:date1];
        if (secondsInterval<0) {
            NSString*msg=[NSString stringWithFormat:@"抱歉，样本%@的抽血时间不能大于入库时间",model.recordId];
            [self.view makeToast:msg];
            return;
        }
        NSString *type=@"";
        if (secondsInterval>2400) {
            type =@"1";
        }else {
            type =@"0";
            
        }
        [types addObject:type];
    }
    
    
    XYSixthStepViewController *XYSixthStepVC = [[XYSixthStepViewController alloc] init];
    XYSixthStepVC.NewRecordArray = self.NewRecordArray;
    XYSixthStepVC.timetypes=types;
    [self.navigationController pushViewController:XYSixthStepVC animated:YES];
}
-(void)cancelclick{
    UIViewController*miloViewControl=(UIViewController *)[[self.navigationController viewControllers]objectAtIndex:1];
    [self.navigationController popToViewController:miloViewControl animated:YES];
}

-(void)reloadtime1{
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    _dateLabel.text=dateString;
}

-(void)creatDetailDict{
    
    NSDictionary *questionDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"5、血液样本送至生物样本库时间", KEY_RECORD_QUESTION,
                                  TYPE_QUESTION_DEFAULT, KEY_RECORD_TYPE,
                                  _dateLabel.text, KEY_RECORD_ANSWER, nil];
    
    NSArray *qustionArr = [NSArray arrayWithObjects:questionDict, nil];
    
    for (Record *model in self.NewRecordArray) {
        NSMutableDictionary *tempDict = [model getDetailDict];
        [tempDict setObject:qustionArr forKey:KEY_STEP];
        [model setDetailDict:tempDict];
        model.recordDetail = [tempDict JSONString];
        model.savedStepCount = 5;

    }
    
}

-(void)nextStepClick{
    XYSixthStepViewController *XYSixthStepVC = [[XYSixthStepViewController alloc] init];
    XYSixthStepVC.NewRecordArray = self.NewRecordArray;
    NSMutableArray *types = [NSMutableArray arrayWithCapacity:0];
    for (Record *model in self.NewRecordArray) {
        NSString*step3date = [[[[model getDetailDict] objectForKey:@"step3"] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER];
        NSString*step5date = [[[[model getDetailDict] objectForKey:@"step5"] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
        NSDate *date1=[formatter dateFromString:step3date];
        NSDate *date2=[formatter dateFromString:step5date];
        NSTimeInterval secondsInterval= [date2 timeIntervalSinceDate:date1];
        NSString *type=@"";
        if (secondsInterval>2400) {
            type =@"1";
        }else {
            type =@"0";
            
        }
        [types addObject:type];
    }
    XYSixthStepVC.fromtype = @"search";
    XYSixthStepVC.timetypes=types;
    [self.navigationController pushViewController:XYSixthStepVC animated:YES];
}

@end
